package edu.csl.study.shardingsphere.hint;

import org.apache.shardingsphere.api.hint.HintManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

@Service
public class HintServiceImpl implements HintService {
    @Autowired
	DataSource dataSource;
	@Override
	public void processWithHintValueForShardingDatabases() throws SQLException, IOException {
		DataSource dataSource2 = null;

		dataSource2 = DataSourceHelper.getDataSourceForShardingDatabases();

		//  dataSource2 = dataSource; 这里也能注入dataSource，为了Demo，所以有了hint文件夹的yaml文件。
		// 当然，重新生成的dataSource的好处是：不会影响原来的分片的数据源。
		// HintManager是ThreadLocal相关的。
		try (HintManager hintManager = HintManager.getInstance();
				Connection connection = dataSource2.getConnection();
				Statement statement = connection.createStatement()) {
			HintManagerHelper.initializeHintManagerForShardingDatabases(hintManager);

			ResultSet result = statement.executeQuery("select user_id, user_name from user");
			
			while (result.next()) {
				System.out.println(result.getLong(1) + ": " + result.getString(2));
            }
		}
	}

	@Override
	public void processWithHintValueForShardingDatabasesAndTables() throws SQLException, IOException {
		DataSource dataSource2 = null;

		dataSource2 = DataSourceHelper.getDataSourceForShardingDatabases();

		//  dataSource2 = dataSource; 这里也能注入dataSource，为了Demo，所以有了hint文件夹的yaml文件。
		// 当然，重新生成的dataSource的好处是：不会影响原来的分片的数据源。
		// HintManager是ThreadLocal相关的。

		try (HintManager hintManager = HintManager.getInstance();
				Connection connection = dataSource2.getConnection();
				Statement statement = connection.createStatement()) {
			HintManagerHelper.initializeHintManagerForShardingDatabasesAndTables(hintManager);

			ResultSet result = statement.executeQuery("select record_id, user_id, remark from health_record");
			
			while (result.next()) {
				System.out.println(result.getLong(1) + ": " + result.getLong(2) + ": " + result.getString(3));
            }
		}		
	}

	@Override
	public void processWithHintValueMaster() throws SQLException, IOException {
		DataSource dataSource2 = null;

//			dataSource2 = DataSourceHelper.getDataSourceForShardingDatabases();

		  dataSource2 = dataSource; // 这里也能注入dataSource，为了Demo，所以有了hint文件夹的yaml文件。
		// 当然，重新生成的dataSource的好处是：不会影响原来的分片的数据源。
		// HintManager是ThreadLocal相关的。
		try (HintManager hintManager = HintManager.getInstance();
				Connection connection = dataSource2.getConnection();
				Statement statement = connection.createStatement()) {
			// 强制走主库。
			HintManagerHelper.initializeHintManagerForMaster(hintManager);

			ResultSet result = statement.executeQuery("select user_id, user_name from user");
			
			while (result.next()) {
				System.out.println(result.getLong(1) + ": " + result.getString(2));
            }
		}		
	}
}
